package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.ServerResponse;
import com.chixing.mapper.MyordersMapper;
import com.chixing.mapper.PersonFlowMapper;
import com.chixing.pojo.Myorders;
import com.chixing.pojo.PersonFlow;
import com.chixing.service.IPersonFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 流水表 服务实现类
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Service
public class PersonFlowServiceImpl  implements IPersonFlowService {

    @Autowired
    private PersonFlowMapper personFlowMapper;

    @Autowired
    private MyordersMapper myordersMapper;

    @Override
    public PersonFlow selectById(Integer id) {
        PersonFlow personFlow = personFlowMapper.selectById(id);
        return personFlow;
    }

    @Override
    public ServerResponse insert(PersonFlow personFlow) {
        //1.添加流水 2.修改订单
        QueryWrapper<Myorders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",personFlow.getText1());
        Myorders myorders = myordersMapper.selectOne(queryWrapper);
        personFlow.setOrderId(myorders.getOrderId());
        QueryWrapper<PersonFlow> queryWrapperFlow = new QueryWrapper<>();
        queryWrapperFlow.eq("text1",personFlow.getText1());
        int insert = personFlowMapper.insert(personFlow);
        PersonFlow flow = personFlowMapper.selectOne(queryWrapperFlow);
        myorders.setOrderState(1);
        int i = myordersMapper.updateById(myorders);
        Map<String,Object> map = new HashMap<>();
        if(i>0&&insert>0){
            map.put("flow",flow);
            map.put("myorders",myorders);
            return ServerResponse.success("成功",map);
        }

        return ServerResponse.success("失败",null);
    }
}
